package org.icress.fusion.moc

import org.springframework.transaction.annotation.*

import org.icress.fusion.shiro.User
import org.apache.shiro.SecurityUtils


class AdjustService {

    static transactional = true

    @Transactional(readOnly = true)
    def list(Object params) {
	    //get current user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        def AdjustInstanceList = Adjust.withCriteria{
			if(params?.max) 
                maxResults(params.int('max'))

			if(params?.offset) 
                firstResult(params.int('offset'))

			if(params?.sort && params?.order) 
                order(params?.sort, params?.order)

            if(currentUser.username != 'admin')
    	        eq("owner", currentUser)
        }
        return AdjustInstanceList
    }

    @Transactional(readOnly = true)
    def count(Object params) {
	    //get current user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        def count = 0
        if(currentUser.username != 'admin'){
    	    count = Adjust.countByOwner(currentUser)
        }else{
            count = Adjust.count()
        }
        
        return count
    }
    
    def save(adjustInstance) {
        //get user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        //set instance params
        adjustInstance.createdBy = currentUser
        adjustInstance.lastModifiedBy = currentUser
        adjustInstance.owner = currentUser
        //save
        return adjustInstance.validate() && adjustInstance.save(flush: true)
    }

    @Transactional(readOnly = true)
    def show(id) {
        //get current user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        def AdjustInstance = Adjust.withCriteria(uniqueResult:true){
            eq("id", id.toLong())
            if(currentUser.username != 'admin')
                eq("owner", currentUser)
        }
        return AdjustInstance
    }

    def edit(id) {
        //get current user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        def adjustInstance = Adjust.withCriteria(uniqueResult:true){
            eq("id", id.toLong())
            if(currentUser.username != 'admin')
                eq("owner", currentUser)
        }
        return adjustInstance
    }

    def update(adjustInstance) {
        //get user
        def currentUser = User.findByUsername(SecurityUtils.getSubject().getPrincipal())
        //set instance params
        adjustInstance.lastModifiedBy = currentUser
        adjustInstance.lastUpdated = new java.util.Date()
        //save
        return adjustInstance.validate() && adjustInstance.save(flush: true)
    }
    
    def delete(adjustInstance) {
        return adjustInstance.delete(flush:true)
    }

    def validateLockdate(date) {
        def ta = Ta.withCriteria(uniqueResult:true){
            order('lockDate', 'desc')
            maxResults(1)
        }
        
        return date >  ta.lockDate
    }

}
